﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.Controls;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Frm
{
    public partial class YaoSuFrmCondition : BaseForm
    {
        DataTable sysCourtTable = new DataTable();
        DataTable sysLJD = new DataTable();
       public Data dta = new Data();
        public YaoSuFrmCondition(DataTable ljddt, DataTable fydt)
        {
            InitializeComponent();
            sysLJD = ljddt;
            sysCourtTable = fydt;
            //cbxCourt.TreeView.CheckBoxes = false;
            if (ljddt != null && ljddt.Rows.Count > 0)
            {
                foreach (DataRow dr in ljddt.Rows)
                {
                    TreeNode node = new TreeNode() { Text = dr["Name"].ToString(), Tag = dr["ID"].ToString() };
                    cbxLJJ.TreeView.Nodes.Add(node);
                }
            }
            if (fydt != null && fydt.Rows.Count > 0)
            {
                sysCourtTable = fydt;
                if (fydt.Select("ParentID=''").Length > 0)
                {
                    DataTable sysCourtdt = fydt.Select(" ParentID='' ").CopyToDataTable<DataRow>();
                    foreach (DataRow dr in sysCourtdt.Rows)
                    {
                        TreeNode node = new TreeNode() { Text = dr["Name"].ToString(), Tag = dr["ID"].ToString() };
                        node.Nodes.Add(new TreeNode());
                        cbxCourt.TreeView.Nodes.Add(node);
                    }
                    cbxCourt.DropDownStyle = ComboBoxStyle.DropDown;
                    cbxCourt.TreeView.AfterExpand += TreeView_AfterExpand; ;
                }

            }
        }

        /// <summary>
        /// 法院节点树展开事件 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TreeView_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            DataTable dt = new DataTable();
            if (sysCourtTable.Select(" ParentID='" + e.Node.Tag.ToString() + "' ").Length > 0)
            {
                dt = sysCourtTable.Select(" ParentID='" + e.Node.Tag.ToString() + "' ").CopyToDataTable<DataRow>();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        TreeNode node = new TreeNode { Text = dr["Name"].ToString(), Tag = dr["ID"].ToString() };
                        if (sysCourtTable.Select(" ParentID='" + node.Tag.ToString() + "' ").Length > 0)
                        {
                            DataTable dtt = sysCourtTable.Select(" ParentID='" + node.Tag.ToString() + "' ").CopyToDataTable<DataRow>();
                            if (dtt != null && dtt.Rows.Count > 0)
                            {
                                node.Nodes.Add(new TreeNode());
                            }
                        }
                        e.Node.Nodes.Add(node);
                    }
                }
            }
        }



        private void btnOK_Click(object sender, EventArgs e)
        {
            StringBuilder sbljj = new StringBuilder();
            if (!string.IsNullOrEmpty(cbxLJJ.Text.Trim()))
            {
                string[] ljds = cbxLJJ.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in ljds)
                {
                    if (sysLJD.Select(" Name='" + item + "'").Length > 0)
                    {
                        sbljj.Append("'" + sysLJD.Select(" Name='" + item + "'")[0]["ID"] + "',");
                    }
                }

            }
            StringBuilder sbfy = new StringBuilder();
            if (!string.IsNullOrEmpty(cbxCourt.Text.Trim()))
            {
                string[] ljds = cbxCourt.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in ljds)
                {
                    if (sysCourtTable.Select(" Name='" + item + "'").Length > 0)
                    {
                        sbfy.Append("'" + sysLJD.Select(" Name='" + item + "'")[0]["ID"] + "',");
                    }
                }

            }
            if (!string.IsNullOrEmpty(sbljj.ToString()))
            {
                dta.Ljd = sbljj.ToString().Remove(sbljj.Length - 1, 1);
            }
            if (!string.IsNullOrEmpty(sbfy.ToString()))
            {
                dta.Fy = sbfy.ToString().Remove(sbfy.Length - 1, 1);
            }
            dta.Wpp = string.IsNullOrEmpty(txbWPP.Text.Trim()) ? 200 : int.Parse(txbWPP.Text.Trim());
            dta.Ppwjy = string.IsNullOrEmpty(txbPPWJY.Text.Trim()) ? 200 : int.Parse(txbPPWJY.Text.Trim());
            dta.Yjy = string.IsNullOrEmpty(txbYJJ.Text.Trim()) ? 200 : int.Parse(txbYJJ.Text.Trim());           
            this.DialogResult = DialogResult.OK;
        }

        private void txbWPP_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }

   
}
